package com.easpeed.comon.utils

import android.content.Context
import androidx.core.util.Consumer
import com.easpeed.comon.bean.Authentication
import com.easpeed.comon.bean.BaseData
import com.easpeed.comon.bean.LoginResult
import com.easpeed.comon.bean.UserOnlineStatus
import com.easpeed.comon.utils.SocketHelper.isUserOnline
import org.greenrobot.eventbus.EventBus
import java.io.File

/**
 * Created by bigyu on 2025-04-07 14:42
 * com.easpeed.comon.utils
 */
class NetHelper(val mContext: Context) {


    private var loading: LoadingDialogUtils? = null

    init {
        loading = LoadingDialogUtils(mContext)
    }


    fun <T> checkAuth(data: BaseData<T>) {
        if (data.code == 600) {
            EventBus.getDefault().post(Authentication(data.message))
        }
    }

    fun netForCreateLine(isLoading: Boolean = true, map: HashMap<String, Any>, consumer: Consumer<BaseData<Line>>) {
        if (isLoading)
            loading?.show()
        ApiHelper.createLine(map) {
            if (isLoading)
                loading?.dismiss()
            consumer.accept(it)
            checkAuth(it)
        }
    }
    fun netForWaveLength(isLoading: Boolean = true, map: HashMap<String, Any>, consumer: Consumer<BaseData<List<String>>>) {
        if (isLoading)
            loading?.show()
        ApiHelper.waveLength(map) {
            if (isLoading)
                loading?.dismiss()
            consumer.accept(it)
            checkAuth(it)
        }
    }

    fun netForCreateRecord(isLoading: Boolean = false, map: HashMap<String, Any?>, consumer: Consumer<BaseData<Record>>) {
        if (isLoading)
            loading?.show()
        ApiHelper.createRecord(map) {
            if (isLoading)
                loading?.dismiss()
            consumer.accept(it)
            checkAuth(it)
        }
    }

    fun netForUpdateRecord(isLoading: Boolean = false, map: HashMap<String, Any?>, consumer: Consumer<BaseData<String>>) {
        if (isLoading)
            loading?.show()
        ApiHelper.updateRecord(map) {
            if (isLoading)
                loading?.dismiss()
            consumer.accept(it)
            checkAuth(it)
        }
    }

    fun netForUpdateLocation(isLoading: Boolean = false, map: HashMap<String, Any?>, consumer: Consumer<BaseData<String>>) {
        if (isLoading)
            loading?.show()
        ApiHelper.uploadLocation(map) {
            if (isLoading)
                loading?.dismiss()
            consumer.accept(it)
            checkAuth(it)
        }
    }

    @JvmOverloads
    fun netForLineList(isLoading: Boolean = false, map: HashMap<String, Any>, consumer: Consumer<BaseData<List<Line>>>) {
        if (isLoading) loading?.show()
        ApiHelper.lineList(map) {
            if (isLoading) loading?.dismiss()
            consumer.accept(it)
            checkAuth(it)
        }
    }

    @JvmOverloads
    fun netForRecordList(isLoading: Boolean = false, id: String, map: HashMap<String, Any>, consumer: Consumer<BaseData<List<Record>>>) {
        if (isLoading) loading?.show()
        ApiHelper.recordList(id, map) {
            if (isLoading) loading?.dismiss()
            consumer.accept(it)
            checkAuth(it)
        }
    }

    fun netForDeleteLine(lineId: String, map: HashMap<String, Any>, consumer: Consumer<BaseData<String>>) {
        loading?.show()
        ApiHelper.deleteLine(lineId = lineId, map) {
            loading?.dismiss()
            consumer.accept(it)
            checkAuth(it)
        }
    }

    fun netForUpdateLine(map: HashMap<String, Any>, consumer: Consumer<BaseData<String>>) {
        loading?.show()
        ApiHelper.updateLine(map) {
            loading?.dismiss()
            consumer.accept(it)
            checkAuth(it)
        }
    }


    fun netForLogin(map: HashMap<String, Any>, consumer: Consumer<BaseData<LoginResult>>) {
        loading?.show()
        ApiHelper.login(map) {
            loading?.dismiss()
            consumer.accept(it)
            checkAuth(it)
        }
    }


    fun netForListOptical(isLoading: Boolean = false, map: HashMap<String, Any>, consumer: Consumer<BaseData<List<Optical>>>) {
        if (isLoading) loading?.show()
        ApiHelper.opticalList(map) {
            if (isLoading) loading?.dismiss()
            consumer.accept(it)
            checkAuth(it)
        }
    }

    fun netForUpdateOptical(map: HashMap<String, Any>, consumer: Consumer<BaseData<Optical>>) {
        loading?.show()
        ApiHelper.updateOptical(map) {
            loading?.dismiss()
            consumer.accept(it)
            checkAuth(it)
        }
    }

    fun netForCreateOptical(isLoading: Boolean = true, map: HashMap<String, Any?>, consumer: Consumer<BaseData<Optical>>) {
        if (isLoading)
            loading?.show()
        ApiHelper.createOptical(map) {
            if (isLoading)
                loading?.dismiss()
            consumer.accept(it)
            checkAuth(it)
        }
    }

    fun netForDeleteOptical(lineId: String, map: HashMap<String, Any>, consumer: Consumer<BaseData<String>>) {
        loading?.show()
        ApiHelper.deleteOptical(lineId = lineId, map) {
            loading?.dismiss()
            consumer.accept(it)
            checkAuth(it)
        }
    }

    fun netForDeleteRecord(lineId: String, map: HashMap<String, Any?>, consumer: Consumer<BaseData<String>>) {
        loading?.show()
        ApiHelper.deleteRecord(lineId = lineId, map) {
            loading?.dismiss()
            consumer.accept(it)
            checkAuth(it)
        }
    }

    fun netForUploadFile(isLoading: Boolean = true, file: File, consumer: Consumer<BaseData<UploadFile>>) {
        if (isLoading)
            loading?.show()
        ApiHelper.uploadFile(file) {
            if (isLoading)
                loading?.dismiss()
            consumer.accept(it)
            checkAuth(it)
        }
    }

}